package com.bamboo.common.annotations.aspect;

import com.bamboo.common.annotations.DemoAnnotation;
import com.bamboo.core.util.AnnotationUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
* @description 
* @date 2025/3/6 10:49
* @author chenzh
*/
@Aspect
@Component
public class DemoAnnotationAspect {
    final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("@annotation(com.bamboo.common.annotations.DemoAnnotation)")
    public void handlerDemoAnnotation() {}

    @Before("handlerDemoAnnotation()")
    public void preHandle(JoinPoint joinPoint) {
        // 获取注解的信息
        logger.info("注解标注的类: {}", joinPoint.getTarget().getClass());
        logger.info("JoinPoint attr#this: {}", joinPoint.getThis());
        logger.info("JoinPoint target annotations: {}", Arrays.stream(joinPoint.getTarget().getClass().getAnnotations()).map(Annotation::getClass).collect(Collectors.toList()));
        DemoAnnotation annotation = (DemoAnnotation) AnnotationUtil.getMethodLevelAnnotation(joinPoint, DemoAnnotation.class);
        logger.info("JoinPoint target specified annotation: {}, value: {}", annotation, annotation.value());
        // 获取类、方法的信息
    }
}
